package com.mew.chess.core.game.qmcs;

import com.mew.chess.core.common.handler.ChessHandlerMapCmdType;
import com.mew.chess.core.common.handler.ChessPlayAnnotation;
import com.mew.chess.core.common.handler.ChessPlayHandler;
import com.mew.chess.core.game.qmcs.game.QmcsGameManager;
import com.mew.chess.core.game.qmcs.game.QmcsRoomManager;
import com.mew.chess.core.game.qmcs.game.QmcsTableManager;
import com.mew.chess.core.game.qmcs.game.common.QmcsTableUtil;
import com.mew.chess.core.game.qmcs.game.message.QmcsCMDMapType;
import com.mew.chess.core.game.qmcs.game.message.request.QmcsHitRequestMessage;
import com.mew.chess.core.game.qmcs.service.QmcsService;
import com.mew.chess.core.game.shbz.game.ShbzGameManager;
import com.mew.chess.core.game.shbz.game.ShbzRoomManager;
import com.mew.chess.core.game.shbz.game.message.request.HitRequestMessage;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.LongMessage;
import com.mew.common.rest.bo.GameSession;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@ChessPlayAnnotation(ChessHandlerMapCmdType.QmcsBet)
@Service
@Slf4j
public class QmcsBetHandler implements ChessPlayHandler<QmcsHitRequestMessage> {

    @Autowired
    private QmcsService qmcsService;


    @Override
    public void handle(ZebraCommand<QmcsHitRequestMessage> command, GameSession gameSession) {


        int roomId = gameSession.getRoomId();
        long userId = gameSession.getUserId();

        QmcsRoomManager manager = QmcsGameManager.getInstance().getController(roomId);
        QmcsHitRequestMessage message = command.getRequest();

        if (message.getBetId()<1||message.getBetId()>manager.getBets().length){
            command.sendFailureCode(ResponseCode.BetError.code());
            return;
        }

        //攻击推送
        QmcsTableManager table = QmcsGameManager.getInstance().getController(roomId).getTableByUserId(userId);
        QmcsTableUtil.sendMsgByTable(table,command.getRequest(), QmcsCMDMapType.HitMessage.code(),userId);

        LongMessage longMessage = qmcsService.bet(userId, roomId, command.getRequest());

        command.sendMessage(longMessage);



    }
}
